package datasource;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DataToMySQL {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");

        //2.获取连接（表示JVM的进程和数据库进程之间的通道打开了，这属于进程之间的通信，使用完后记得关闭通道）。
        /**
         *  url: jdbc:mysql://192.168.220.100:3306/bigdata29?characterEncoding=UTF-8
         */
        Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.220.100:3306/szt?characterEncoding=UTF-8&useSSL=false", "root", "123456");
        System.out.println("成功与mysql数据库建立连接：" + conn);

        //3.获取数据库操作对象（专门执行sql语句的对象）
        Statement state = conn.createStatement();

        //4、执行sql语句
        BufferedReader br = new BufferedReader(new FileReader("/usr/local/soft/bigdata29/szt/data/ods_data/1714964473339-3"));


        String line = null;
        while ((line = br.readLine()) != null) {
            String[] datas = line.split(",");
            String sql1 = "insert into szt_data values('"+datas[0]+"','"+datas[1]+"','"+datas[2]+"','"
                    +datas[3]+"','"+datas[4]+"','"+datas[5]
                    +"','"+datas[6]+"','"+datas[7]+"','"+datas[8]+"','"+datas[9]+"','"+datas[10]+"')";
            int i = state.executeUpdate(sql1); // Affected rows: 1
        }
        System.out.println("数据添加成功！");

        br.close();
    }
}
